package com.ionehe.group.common.uaa.ex.dto;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SpringSecurity需要的用户详情
 * Created by 秀 on 2018/4/26.
 */
public class AdminUserDetails implements UserDetails {
    private ManageInfoDTO manageInfoDTO;
    private List<FunctionDTO> functionDTOList;
    public AdminUserDetails(ManageInfoDTO manageInfoDTO, List<FunctionDTO> functionDTOList) {
        this.manageInfoDTO = manageInfoDTO;
        this.functionDTOList = functionDTOList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的角色
        return functionDTOList.stream()
                .map(fun ->new SimpleGrantedAuthority(fun.getUrl()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return manageInfoDTO.getPassword();
    }

    @Override
    public String getUsername() {
        return manageInfoDTO.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
